<template>
  <!--设备事件理模块-->
  <div class="app-container">
    <!--查询条件区-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="设备名称" prop="devName">
        <el-input
          v-model="queryParams.devName"
          placeholder="请输入设备名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--快捷按钮区-->
    <!--表格数据区-->
    <el-table v-loading="loading" :data="eventList">
      <el-table-column label="设备名称" align="center" key="devName" prop="devName"/>
      <el-table-column label="事件时间" align="center" key="createDate" prop="createDate"/>
      <el-table-column label="事件标题" align="center" key="title" prop="title"/>
      <el-table-column label="事件描述" align="center" key="callBody" prop="callBody"/>
      <el-table-column label="事件状态" align="center" key="status" prop="status"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleXiangxi(scope.row)"
          >查看事件详情</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-timer"
            @click="handleTest(scope.row)"
          >自检</el-button> -->
        </template>
      </el-table-column>
      <!-- </el-table> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>

    <!-- 查看明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body @close="cancel">
      <div class="line">设备名称：{{event.devName}}</div>
      <div class="line">事件时间：{{event.createDate}}</div>
      <div class="line">事件标题：{{event.title}}</div>
      <div class="line">事件描述：{{event.callBody}}</div>
      <div class="line">事件状态：{{event.status}}</div>
      <div class="line">推送地址：{{event.callUrl}}</div>
      <div class="line">事件接收：</div>
      <pre class="json">{{receiverObj}}</pre>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 轮询测试弹框 -->
    <!-- <el-dialog :title="title_timer" :visible.sync="open_timer" width="60%" append-to-body @close="cancel_timer">
      <div class="line1">请点击设备的自检消音按钮</div>
      <div class="line2" v-if="isDetected">已检测到</div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel_timer()">{{isDetected==true?'关闭':'中断'}}</el-button>
      </div>
    </el-dialog> -->
   </div>
</template>

<script>
import {listEvent,getEvent} from "@/api/business/event";
export default {
  name: "Event",
  dicts: ['ga_sktr_pe_status','ga_sktr_pep_status'], //事件状态
  data() {
    return {
      loading: true, //表格遮罩层(用于表格刷新必须的)
      showSearch: true, //表格上方的大查询条件参数（是否显示搜索条件）（如果表格上方有查询条件并且有类似51行处的显示隐藏按钮，这个必须）
      queryParams: {  //表格上方的大查询条件参数
        pageNum: 1,
        pageSize: 10,
        devName: undefined,
      },
      total: 0, //表格总条数（如果有分页，这个必须）
      eventList: [], //事件表格数据

      //弹出层相关
      title: "", //明细弹出层标题
      open: false, //是否显示弹出层
      event:{}, //用于显示事件的详情

      receiverObj:{} ,//用于保存转换后的json对象

      //定时器弹出层相关变量
      title_timer: "",
      open_timer:false, //是否显示定时器弹层
      isDetected:false, //当轮询接口有返回时，用此接收，并显示到弹层上，同时关闭定时器
      int_timer: "", //终止定时器控制变量
      //在此之上补充页面属性
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //查询订单列表
    getList() {
      this.loading = true;
      listEvent(this.queryParams).then(response => {
        console.log("获取事件列表接口返回值==",response.rows);
        this.eventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //表格上方，点击搜索按钮操作（表格搜索）
    handleQuery() {
      this.queryParams.pageNum=1;
      this.getList();
    },
    //表格上方，点击重置按钮操作
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //表格每行数据，点击"查看详细"按钮操作
    handleXiangxi(row) {
      var that=this;
      const deventId = row.deventId;
      this.title = "事件详细信息";
      this.open = true;
      getEvent(deventId).then(response => {
        console.log("根据id获取事件信息的返回值==",response.data);
        that.event=response.data;
        that.receiverObj=JSON.parse(that.event.receiver);
        console.log("转换后的event.receiver字段==",that.receiverObj);
      });
    },
    //定时器轮询接口
    handleTest(row) {
      var that=this;
      console.log("传来的行数据==",row);
      this.title_timer=row.devName+"设备定时监测";
      this.open_timer=true;
      var query={
        "title":row.title,
        "devName":row.devName,
        "recent":300000,
      }
      that.int_timer=setInterval(()=>{
        if(that.isDetected){
          clearInterval(that.int_timer); //终止定时器
        }
        listEvent(query).then(response => {
          console.log("设备定时监测接口返回值==",response.rows);
          var arr=response.rows;
          if(arr.length>0){ //还需要加上callBody=="测试"  这个条件
            for(var i=0;i<arr.length;i++){
              if(arr[i].callBody.indexOf("测试")!=-1){
                that.isDetected=true;
                break;
              }
            }
          }
          console.log("handleTest方法的定时器执行中that.int_timer==",that.int_timer,that.isDetected,new Date());
        });
      },10000); //每隔20秒执行一次

    },
    //对话框全局操作
    //取消按钮
    cancel() {
      this.open=false;
      this.event={};
      this.receiverObj={};
    },

    cancel_timer(){
      this.open_timer=false;
      //销毁定时器
      if(this.int_timer){
      	clearInterval(this.int_timer);
      }
      console.log("cancel_timer方法中this.int_timer==",this.int_timer);
      this.isDetected=false;
    },
    //在此上方补充方法
  }
};
</script>
<style scoped lang="scss">
.json {
  background-color: #f0f0f0;
  padding: 10px;
  color: #333;
  font-family: monospace;
}
.line{
  width: 100%;
  min-height: 30px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
  font-size: 16px;
  font-family: MicrosoftYaHei;
  /*font-weight: bolder;*/
  text-align: left;
  line-height: 30px;
}
/*
.line1{
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  font-size: 26px;
  font-family: MicrosoftYaHei;
  text-align: left;
  line-height: 50px;
}
.line2{
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 36px;
  font-family: MicrosoftYaHei;
  font-weight: bolder;
  text-align: left;
  line-height: 80px;
}
*/
</style>

